home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / misc1s.zoo / misc1 / combine / combine2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-23  |  3.6 KB  |  211 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "util.h"
  4. #include "os_dep.h"
  5. /*
  6.  * main: Main program for the COMBINE2 utility
  7.  *
  8.  * The COMBINE2 utility converts the 'HED' output file of the COMBINE
  9.  * utility to be a standard symbolic file.
  10.  *
  11.  * Return value:
  12.  *      This procedure has no return value.
  13.  */
  14. void main (argc, argv)
  15. int     argc;            /* command line argument count */
  16.  
  17. char  **argv;            /* command line arguments */
  18.  
  19. {
  20.  
  21.     int     buf_len;    /* Actual number of characters in buffer */
  22.  
  23.     bool delete;        /* TRUE if currently deleteing lines */
  24.  
  25.     int     file_count;    /* Number of file arguments */
  26.  
  27.     int     i;        /* Misc. variable */
  28.  
  29.     FILE * in_file;        /* input file */
  30.  
  31. #define LINE_LENGTH 512
  32.     char    mbuffer[LINE_LENGTH];/* Line buffer */
  33.  
  34.     FILE * out_file;    /* Output file */
  35.  
  36.     int     status;        /* Misc. status */
  37.  
  38.     extern  void error ();
  39. #ifndef VOS
  40.     extern int      errno;
  41. #endif
  42.  
  43.  
  44.  
  45.     /*
  46.      * Handle each command line argument.
  47.      */
  48.     in_file = stdin;
  49.     out_file = stdout;
  50.  
  51.     file_count = 0;
  52.  
  53.     for (i = 1; i < argc; ++i) {
  54.  
  55.         /*
  56.          * Handle options arguments.
  57.          */
  58.         if (argv[i][0] == '-' && argv[i][1] != '\0') {
  59.  
  60.             sprintf (mbuffer,
  61.                 "Unrecognized option '%s' specified", argv[i]);
  62.             error (mbuffer);
  63.  
  64. #ifdef VOS
  65.  
  66.         /*
  67.          * Handle redirections of 'stdin':
  68.          *
  69.          * This code won't get executed on a UNIX O.S. However, on
  70.          * VOS this code allows the same syntax to work.
  71.          */
  72.         } else if (argv[i][0] == '<' && argv[i][1] != '\0') {
  73.             in_file = fopen (&argv[i][1], "r");
  74.  
  75.             if (in_file == 0) {
  76.                 perror( &argv[i][1]);
  77.                 exit(2);
  78.             }
  79.  
  80.         /*
  81.          * Handle redirections of 'stdout':
  82.          *
  83.          * This code won't get executed on a UNIX O.S. However, on VOS this
  84.          * code allows the same syntax to work.
  85.          */
  86.  
  87.         } else if (argv[i][0] == '>' && argv[i][1] != '\0') {
  88.             out_file = fopen (&argv[i][1], "w");
  89.  
  90.             if (out_file == 0) {
  91.                 perror( &argv[i][1] );
  92.                 exit( 2);
  93.             }
  94. #endif
  95.  
  96.         /*
  97.          * Handle file arguments not preceeded by a specific
  98.          * option argument.
  99.          */
  100.         } else {
  101.  
  102.             if (file_count >= 2) {
  103.                 error ("Too many files specified");
  104.             }
  105.  
  106.             if (file_count == 0) {
  107.                 in_file = fopen (argv[i], "r");
  108.  
  109.                 if (in_file == 0) {
  110.                     perror(argv[i]);
  111.                     exit( 2 );
  112.                 }
  113.  
  114.             } else {
  115.  
  116.                 out_file = fopen (argv[i], "w");
  117.  
  118.                 if (out_file == 0) {
  119.                     perror(argv[i]);
  120.                     exit(2);
  121.                 }
  122.  
  123.             }
  124.  
  125.             file_count++;
  126.         }
  127.     }
  128.  
  129.     /*
  130.      * Read the next line of the file.
  131.      */
  132. #ifdef VOS
  133.     out_file -> carriage_control = FALSE;
  134. #endif
  135.  
  136.     delete = FALSE;
  137.  
  138.     for (;;) {
  139.  
  140.         status = os_dep_file_read (in_file, mbuffer, LINE_LENGTH,
  141.             &buf_len);
  142.  
  143.         if (status != SS_NORMAL) {
  144.             if (status == SS_EOT) {
  145.                 exit (0);
  146.             } else {
  147.                 perror( "input file" ) ;
  148.                 exit( 2 );
  149.             }
  150.         }
  151.  
  152.         if (buf_len > 0 && mbuffer[0] == '~') {
  153.  
  154.             if (buf_len > 1) {
  155.  
  156.                 if (mbuffer[1] == '~') {
  157.  
  158.                     if (buf_len > 2) {
  159.                         if (mbuffer[2] == 'I') {
  160.                             continue;
  161.                         } else if (mbuffer[2] == 'D') {
  162.                             delete = TRUE;
  163.                             continue;
  164.                         }
  165.                     }
  166.  
  167.                 } else if (mbuffer[1] == 'E') {
  168.                     delete = FALSE;
  169.                     continue;
  170.                 }
  171.  
  172.             }
  173.  
  174.         }
  175.  
  176.         if (!delete) {
  177.             status = os_dep_file_write (out_file, mbuffer, buf_len);
  178.  
  179.             if (status != SS_NORMAL) {
  180. #ifdef VOS
  181.                 sprintf (mbuffer,
  182.                     "Write error on output file: %d\n",
  183.                     status);
  184.                 error (mbuffer);
  185. #else
  186.                 perror( "output file" ) ;
  187. #endif
  188.             }
  189.         }
  190.  
  191.     }
  192.  
  193. }
  194. /*
  195.  * error: output fatal error message
  196.  *
  197.  * This routine outputs an error message and terminates.
  198.  *
  199.  * Return value:
  200.  *      This procedure has no return value.
  201.  */
  202. void error (error_ptr)
  203. char   *error_ptr;        /* input -- Record to output. */
  204.  
  205. {
  206.  
  207.     fprintf (stderr, "combine2: %s.\n", error_ptr);
  208.     exit (2);
  209.  
  210. }
  211.